<template>
	<view class="work-container">
		<uni-table border stripe emptyText="暂无更多数据">
			<!-- 表头行 -->
			<uni-tr>
				<!-- <uni-th align="center">物料</uni-th> -->
				<uni-th align="center" max-width="200">条码</uni-th>
				<uni-th width="50" align="center">删除</uni-th>
			</uni-tr>
			<!-- 表格数据行 -->
			<uni-tr v-for="(item, index) in materials" :key="item.id">
				<!-- <uni-td>{{ item.name }}</uni-td> -->
				<uni-td>{{ item.barcode }}</uni-td>
				<uni-td align="center">
					<view @click.stop="removeRow(index)">-</view>
				</uni-td>
			</uni-tr>
		</uni-table>
		<br>
		<button @click="scanCode">扫 码</button>
		<br>
		<button type="primary" @click="submitStorage">包装入库</button>
		<uni-card :title="'单号: ' + this.detail.vbillcode" :extra="formateDate(this.detail.createTime)"
			style="margin: 15px 0 0 0;">
			<text class="title-text">
				产品：{{this.detail.productName}}；排产数：{{this.detail.dispachNum}}；
			</text>
		</uni-card>
	</view>
</template>

<script>
	import {
		parseTime
	} from "@/utils/ruoyi.js"
	import {
		g2bind
	} from "@/api/pro_package/get_list.js"

	var barcodeModel = uni.requireNativePlugin("iData-BarcodePlugin-BarcodeModule");
	const modal = uni.requireNativePlugin('modal');
	var globalEvent = uni.requireNativePlugin('globalEvent');
	export default {
		data() {
			return {
				funType: "",
				detail: null,
				materials: [],
				sellcode: "扫壳码",
				silkCodes: [],
			}
		},
		onLoad() {
			// const detail = uni.getStorageSync('currentDetail');
			// this.detail=detail			
			// // 兼容本地测试
			// console.log("detail:"+detail.vbillcode)
			// console.log('detail=' + JSON.stringify(detail));
			// uni.removeStorageSync('currentDetail')

			var _this = this;
			const detail = uni.getStorageSync('currentDetail');
			if (detail == null || detail == undefined || detail == "") {
				_this.detail = {
					vbillcode: "",
					barcode: null,
					id: 0,
					createTime: 1000000000000,
					reportCheckLines: [{
						barcode: null,
						pkCheck: null
					}]
				}
			} else {
				_this.detail = detail
			}
			// 兼容本地测试
			// console.log("detail++++++++")
			// console.log( _this.detail)
			// console.log('detail=' + JSON.stringify(detail));
			uni.removeStorageSync('currentDetail')

			//监听扫码动作。。。根据临时字符串判断是摁了哪个按钮
			globalEvent.addEventListener('iDataBarcodeEvent', function(e) {
				//判断是否重复扫描
				for (let i = 0; i < _this.materials.length; i++) {
					if (e.barcode == _this.materials[i].barcode) {
						uni.showToast({
							title: "重复扫码",
							icon: 'error'
						})
						return;
					}
				}
				_this.materials.push({
					id: _this.materials.length,
					pkCheck: _this.detail.id,
					barcode: e.barcode
				})
			});

			barcodeModel.initScan((ret) => {});
		},
		onUnload() {
			barcodeModel.closeScan((ret) => {
				// modal.toast({
				// 	message: ret,initScan
				// 	duration: 1.5
				// });
			});
			globalEvent.removeEventListener('iDataBarcodeEvent');
		},
		methods: {
			scanCode() {
				var _this = this;
				barcodeModel.scanStart((ret) => {
					_this.funType = "s";
				});
			},
			submitStorage() {

				// TODO: 实现绑定逻辑
				var _this = this;
				
				if (_this.materials.length == 0) {
					uni.showToast({
						title: "请扫码",
						icon: 'error'
					})
					return
				}
				_this.detail.barcode = null;
				_this.detail.createTime = null;
				_this.detail.produceTime = null;
				_this.detail.reportCheckLines = _this.materials;

				g2bind(_this.detail).then(response => {
					_this.materials = null
					_this.materials = []
					// console.log(response);
					this.$modal.msgSuccess("包装入库成功")
				})

			},
			removeRow(index) {
				this.materials.splice(index, 1)
			},
			formateDate(ttt) {
				return parseTime(ttt).slice(2, 10);
			}
		}
	}
</script>

<style lang="scss">
	button {
		margin-bottom: 10px;
	}

	.work-container {
		padding: 15px;
	}

	.title-text {
		font-size: 28rpx;
		white-space: normal;
		/* 允许换行 */
		word-wrap: break-word;
		/* 允许长单词或 URL 换行 */
		/* 如果父容器有固定宽度，确保宽度生效 */
	}

	.multiline-text {
		font-size: 24rpx;
		color: #777;
		white-space: normal;
		/* 允许换行 */
		word-wrap: break-word;
		/* 允许长单词或 URL 换行 */
		/* 如果父容器有固定宽度，确保宽度生效 */
	}
</style>